// pages/contract/contract_main.js
const app = getApp();
const request = require('../../../utils/requestAjax.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    checked:true,
    certificateList:[],
    pageNum: 1,
    //每页数据条数
    dataNum: 20,
    // //控制显示没有更多了
    // noDataLableShow: false,
    certType: '',
    certId:'',
    certName:'',
    certUnit:'',
    projectName:'',
    certTypeName: '',
    type:'',
    beginDate: '',
    endDate:'',
    userInfo: {},
    //是否显示无数据图片
    isNoDataImgShow: false,
    hasMore: false,
    scrollContainerHeight: '100.1%'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({  
      title: options.title
    })
    if(options && options.type==2){
      this.setData({
        certName: options.key,
      })
      
    }else if(options && options.type==1){
      this.setData({
        certId: options.cert_id,
        certName: options.cert_name,
        projectName: options.project_name,
        certUnit: options.cert_unit,
        beginDate: options.begin_date,
        endDate: options.end_date,
        certType: options.cert_type
      })
    }
    if (options && options.cert_type) {
      this.setData({
        certType: options.cert_type
      })
      var certTypeList = wx.getStorageSync('baseData')['3']
      certTypeList.forEach(element => {
        if (element.codeDetail == this.data.certType) {
          this.setData({
            certTypeName: element.codeContent1
          })
        }
      })
    }
    // if (options && options.begin_date) {
    //   this.setData({
    //     beginDate: options.begin_date
    //   })
    // }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNum: 1
    })
    this.getCertificateList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      pageNum: 1
    })
    this.getCertificateList(function () {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMore) {
      this.setData({
        pageNum: this.data.pageNum + 1
      })
      this.getCertificateList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  goCertificateDetail: function (e) {
    let pageUrl = ''
    let roleId=1
    if (roleId == 1) {
      pageUrl = 'certificate_change/certificate_update'
   // } else if (this.data.userInfo.roleId == 2) {
    } else if (roleId == 2) {
      pageUrl = 'certificate_filter/certificate_detail'
    }
    // console.log("pageUrl=" + pageUrl)
    wx.navigateTo({
      url: '/pages/certificate/' + pageUrl + '?id=' + e.currentTarget.dataset.id
    })
  },
  getCertificateList: function (then) {
    let _this = this
    request.requestAjax("/getCertificateQueryList", {
        "certType": this.data.certType,
        "certId":this.data.certId,
        "certName": this.data.certName,
        "projectName":this.data.projectName,
        "certUnit":this.data.certUnit,
        "endDate":this.data.endDate,
        "beginDate": this.data.beginDate,
        "pageNum": this.data.pageNum,
        "dataNum": this.data.dataNum
      }, "正在加载",
      //请求成功回调
      function (res) {
        // console.log(res)
        //如果是第一页直接替换
        if (_this.data.pageNum == 1) {
          _this.setData({
            certificateList: res.data.certificateList,
            hasMore: res.data.hasMore
          })
          if (_this.data.certificateList.length == 0) {
            _this.setData({
              isNoDataImgShow: true,
              scrollContainerHeight: '100%',
              hasMore: false
            })
          } else {
            _this.setData({
              scrollContainerHeight: '100.1%'
            })
          }
        }
        //如果不是第一页
        else {
          //如果还有值 添加进原数组
          if (res.data.certificateList.length != 0) {
            _this.setData({
              certificateList: _this.data.certificateList.concat(res.data.certificateList),
              hasMore: res.data.hasMore
            })

            _this.setData({
              scrollContainerHeight: 'auto'
            })

          } else {
            _this.setData({
              hasMore: res.data.hasMore
              // noDataLableShow: true
            })
          }
        }
        if (then) {
          then()
        }
      },
      //请求失败回调
      function (res) {
        wx.showModal({
          title: '系统提示',
          content: res.msg,
          showCancel: false,
          success(res) {
            if (res.confirm) {} else if (res.cancel) {}
          }
        })
        if (then) {
          then()
        }
      })
  },

  onFilter: (e) => {
    wx.navigateTo({
      url: '/pages/certificate/certificate_filter/certificate_filter'
    })
  }
})